package com.xuetang9.controller.al.way;

import com.github.pagehelper.PageHelper;
import com.xuetang9.domain.Way;
import com.xuetang9.dto.InputDto;
import com.xuetang9.dto.a.ListRequestDTO;
import com.xuetang9.dto.al.MsgDto;
import com.xuetang9.dto.al.WayDTO;
import com.xuetang9.service.WayService;
import com.xuetang9.vo.JsonResultVO;
import com.xuetang9.vo.ListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : allende
 * @describe : 处理前端路线查询删除处理请求
 * @date : 2020/5/13 15:57
 * @vision : 1.0.0
 **/
@Controller("WayControllerAl")
@CrossOrigin //处理跨域请求问题
@ResponseBody //将java转化为json对象
public class WayController {
    /**
     * 处理请求处理对象
     */
    @Autowired
    @Qualifier("WayServiceAl")
    WayService wayService;

    /**
     * 用来查询路线信息请求
     * @param way 路线查询请求参数
     * @return 前端路线查询显示类容
     */
    @GetMapping("/way/list")
    public JsonResultVO listWay(@RequestBody ListRequestDTO<Way> way){
        JsonResultVO<ListVO> jsonResultVO = new JsonResultVO<>();
        // 用来保存需要返回的数据
        ListVO listWayVO = new ListVO();
        // 传入空直接返回空防止资源浪费
        if(way==null){
            return null;
        }
        InputDto inputDto = new InputDto();
        inputDto.function = "查询用户";
        inputDto.date = way;
        // 开启分页
        PageHelper.startPage(way.page,5);
        // 得到查询的结果
        listWayVO = wayService.listWayA(inputDto);
        // 如果得到的结果是空则返回空
        if(listWayVO.getResult() == null ){
            return null;
        }

        // 查询结果保存到需要返回的对象中
        jsonResultVO.setCode(0);
        listWayVO.setResult(new ArrayList<WayDTO>());
        List<WayDTO> wayDTOS=(List<WayDTO>)listWayVO.getResult();

        jsonResultVO.setData(listWayVO);
        return jsonResultVO;
    }



    @RequestMapping(value = "/way/delete",method = RequestMethod.POST)
    public String userDelete(@PathVariable int id, Model model){
        try{
            wayService.deleteWay(id);
            model.addAttribute("success",new MsgDto("","删除成功"));
        }catch (Exception e){
            model.addAttribute("error",new MsgDto("","删除失败")+e.getMessage());
        }

        return "redirect:/way/list";
    }


}
